const pathType = {
  ANSWER: 'answer',
  QUESTION: 'question',
  ZVIDEO: 'zvideo',
  ARTICLE: 'p',
  PIN: 'pin',
  EDU_SECTION: 'section_id',
  EDU_COURSE: 'education/video-course',
  TOPIC: 'topic',
  SOUGOU_BAIKE: 'sougou_baike',
  ZVIDEO_COLLECTION: 'zvideo_collection',
};

export const getUrl = (type, token) => {
  if (type === 'ARTICLE') {
    return `https://zhuanlan.zhihu.com/p/${token}`;
  } else if (type === 'EDU_SECTION')
    return `https://www.zhihu.com/education/video-course/${token.parent?.basic?.out_id}?${pathType[type]}=${token.out_id}`;
  return `https://www.zhihu.com/${pathType[type]}/${token}`;
};

export const getUrlParams = () => {
  if (!window.location.search) return {};
  const urlSearch = window.location.search.slice(1).split('&');
  const result = {};
  urlSearch.forEach((item) => {
    const temp = item.split('=');
    result[temp[0]] = temp[1];
  });
  return result;
};

const pathOfType = {
  p: 'ARTICLE',
  answer: 'ANSWER',
  question: 'QUESTION',
  zvideo: 'ZVIDEO',
  people: 'USER',
  org: 'USER',
  pin: 'PIN',
  section_id: 'EDU_SECTION',
  'video-course': 'EDU_COURSE',
  topic: 'topic',
  sougou_baike: 'sougou_baike',
  zvideo_collection: 'zvideo_collection',
};

/**
 *
 * @param {*} zhihuUrl
 * @returns
 * @description url 解析
 */
export const parseZhihuUrl = (zhihuUrl) => {
  try {
    const newUrl = zhihuUrl.startsWith('https://')
      ? zhihuUrl
      : 'https://' + zhihuUrl;
    const url = new URL(newUrl);
    if (
      url.hostname !== 'www.zhihu.com' &&
      url.hostname !== 'zhuanlan.zhihu.com'
    ) {
      return null;
    }
    // 处理新类型-教育小结
    if (url.search) {
      const query = url.search.slice(1).split('=');
      const objectType = pathOfType[query[0].toLowerCase()];
      if (objectType) {
        // 是通过query区分的类型
        return {
          objectType,
          token: query[1],
        };
      }
    }

    const parts = url.pathname.split('/');
    if (parts.length < 2) {
      return null;
    }
    const objectType = pathOfType[parts[parts.length - 2].toLowerCase()];
    if (!objectType) {
      return null;
    }
    return {
      objectType,
      token: parts[parts.length - 1],
    };
  } catch (e) {
    return null;
  }
};

export const validatePeopleUrl = (rules, value, callback) => {
  if (!value) {
    callback();
    return;
  }

  const url = parseZhihuUrl(value);
  if (url && url.objectType === 'USER') {
    callback();
  } else {
    callback(new Error('请输入正确的知乎个人主页 URL'));
  }
};

export const validateContentUrl = (rules, value, callback) => {
  if (!value) {
    callback();
    return;
  }

  const url = parseZhihuUrl(value);
  if (url && url.objectType !== 'USER') {
    callback();
  } else {
    callback(new Error('请输入正确的知乎内容 URL'));
  }
};

export const validateCommentUrl = (rules, value, callback) => {
  if (!value) {
    callback();
    return;
  }
  const url = parseZhihuUrl(value);
  if (url && url.objectType !== 'USER') {
    callback();
  } else {
    callback(new Error('请输入正确的知乎评论 URL'));
  }
};
